package com.cheejee.goodscenter.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cheejee.goodscenter.module.entity.CategoryPropertiesTemplate;
import com.cheejee.goodscenter.module.response.BaseReponse;
import com.cheejee.goodscenter.module.response.PageReponse;
import com.cheejee.goodscenter.module.vo.CategoryPropertiesTemplateVo;
import com.cheejee.goodscenter.service.CategoryPropertiesTemplateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author CARRY
 * @Date 2020年10月30日下午4:01:31
 */
@RestController
@RequestMapping("/category/template")
@Api(tags = "分类属性模板")
public class CategoryPropertiesTemplateController {
	@Resource
	private CategoryPropertiesTemplateService service;

	@GetMapping("/")
	@ApiOperation(value = "按条件查询属性模板", notes = "多个条件AND相连。比如groupId=1 AND required=true。条件全空查全部。")
	@ApiImplicitParams({ @ApiImplicitParam(name = "categoryId", value = "分类ID", dataType = "int"), 
		    @ApiImplicitParam(name = "groupId", value = "属性分组ID", dataType = "int"),
			@ApiImplicitParam(name = "required", value = "是否必填", dataType = "boolean"), 
			@ApiImplicitParam(name = "initial", value = "属性名首字母，传入other查询其他开头的分类属性模板", dataType = "string"),
			@ApiImplicitParam(name = "type", value = "属性类型，1=商品属性模板，2=SKU属性模板", dataType = "byte"),
			@ApiImplicitParam(name = "pageSize", value = "每页内容数", defaultValue = "0", dataType = "int"), 
			@ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "0", dataType = "int") })
	public Object getByCategory(Integer categoryId, Integer groupId, Boolean required, String initial, Byte type, int pageNum, int pageSize) {

		return PageReponse.ok("查询完成", service.queryCategoryPropertiesTemplate(categoryId, groupId, null, required, type, initial, pageNum, pageSize));
	}

	
	@PutMapping("/")
	@ApiOperation(value = "新增分类属性模板")
	public Object addTemplate(@RequestBody CategoryPropertiesTemplateVo template) {
		CategoryPropertiesTemplate b = CategoryPropertiesTemplateVo.convert(null, template);
		service.addCategoryPropertiesTemplate(b);
		
		return BaseReponse.ok("新增完成", service.queryCategoryPropertiesTemplateById(b.getId()));
	}
	
	@PutMapping("/{id}")
	@ApiOperation(value = "修改分类属性模板")
	public Object changeTemplate(@PathVariable Integer id, @RequestBody CategoryPropertiesTemplateVo template) {
		CategoryPropertiesTemplate b = CategoryPropertiesTemplateVo.convert(id, template);
		service.changeCategoryPropertiesTemplate(b);
		
		return BaseReponse.ok("修改完成", service.queryCategoryPropertiesTemplateById(id));
	}
	
	@DeleteMapping("/{id}")
	@ApiOperation(value = "删除分类属性模板")
	public Object deleteTemplate(@PathVariable Integer[] id) {
		service.deleteCategoryPropertiesTemplate(id);
		
		return BaseReponse.ok("删除完成");
	}
}
